//
//  AddView.m
//  ClassifyShop
//
//  Created by yuyao on 15/3/2.
//  Copyright (c) 2015年 yuyao. All rights reserved.
//

#import "AddView.h"
#import "Masonry.h"
#import "CustomAddButton.h"
#import "GUIDefine.h"
@implementation AddView
@synthesize myTable,delegate,items,imageItems,tagsArray;

- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{
    // 创建一个bitmap的context
    // 并把它设置成为当前正在使用的context
    UIGraphicsBeginImageContext(size);
    // UIGraphicsBeginImageContextWithOptions(size, NO, 1.0);
    // 绘制改变大小的图片
    [img drawInRect:CGRectMake(0, 0, size.width, size.height)];
    // 从当前context中创建一个改变大小后的图片
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
    // 返回新的改变大小后的图片
    return scaledImage;
}

- (void)refreshAddButtons{
    

    [self setTagsArray:(NSMutableArray *)[appDelegate getTags]];
    
    
    [tagsArray setArray:[tagsArray sortedArrayUsingSelector:@selector(compare:)]];
    NSLog(@"tagsArray=%@",[tagsArray sortedArrayUsingSelector:@selector(compare:)]);

    [myTable reloadData];
}
- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor =  [[UIColor blackColor] colorWithAlphaComponent:0.5];
        
        items = [NSMutableArray arrayWithObjects:@"推荐",@"餐厅",@"即享美食",@"购物车",@"收藏",@"最近浏览",@"订单", nil];
        imageItems = [NSMutableArray arrayWithObjects:@"餐厅.png",@"餐厅.png",@"即享美食.png",@"购物车.png",@"收藏.png",@"最近浏览.png",@"订单.png", nil];
       // [middleView setBackgroundColor:[[UIColor alloc] initWithPatternImage:[appDelegate scaleToSize:[UIImage imageNamed:@"1 大框.png"] size:CGSizeMake(300, 420)]]];

        myTable = [[UITableView alloc] init];
        [myTable setBackgroundColor:[[UIColor alloc] initWithPatternImage:[self scaleToSize:[UIImage imageNamed:@"1 大框.png"] size:CGSizeMake(self.frame.size.width-60, 44*(items.count))]]];
        [myTable setDataSource:self];
        [myTable setDelegate:self];
        [myTable setScrollEnabled:NO];
        [self addSubview:myTable];

        int offset= 0;
        if ([UIScreen mainScreen].bounds.size.height>480) {
            offset = 90;
        }else{
            offset = 40;
        }
        
        [myTable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.left.equalTo(self.mas_left).with.offset(30);
            make.top.equalTo(self.mas_top).with.offset(offset);
            make.bottom.equalTo(self.mas_top).with.offset(offset+44*(items.count));
        }];
        
        UIButton *removeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [removeButton setBackgroundImage:[UIImage imageNamed:@"取消按钮.png"] forState:UIControlStateNormal];
        [removeButton addTarget:self action:@selector(remove) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:removeButton];
        
        [removeButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(myTable.mas_bottom).with.offset(30);
            make.centerX.equalTo(myTable);
            make.size.mas_equalTo(CGSizeMake(40, 40));
        }];
        
        
    }
    return self;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return items.count;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"mycell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 25, 25)];
        
        [image setTag:99];
        
        [cell.contentView addSubview:image];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(60, 5, 200, 35)];
        [label setTag:100];
        [label setTextColor:[UIColor grayColor]];
        [label setFont:[UIFont fontWithName:@"Arial" size:13]];
        [cell.contentView addSubview:label];
        
        
        CustomAddButton *addButton = [CustomAddButton buttonWithType:UIButtonTypeCustom];
        [addButton setFrame:CGRectMake(myTable.frame.size.width-60, 12, 40, 20)];
        [addButton setBackgroundImage:[UIImage imageNamed:@"添加按钮.png"] forState:UIControlStateNormal];
        [addButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
        [addButton setTag:101];
        [addButton setViewId:(int)(101+indexPath.row)];
        [cell.contentView addSubview:addButton];

        
        
    }
    [cell setBackgroundColor:[UIColor clearColor]];
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    UIImageView *imageView = (UIImageView *)[cell.contentView viewWithTag:99];
    UIImage *image = [UIImage imageNamed:[imageItems objectAtIndex:indexPath.row]];
    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cell.contentView).with.offset(20);
        make.centerY.equalTo(cell.contentView);
        make.size.mas_equalTo(CGSizeMake(image.size.width/2.5, image.size.height/2.5));
    }];
    [imageView setImage:image];
    
    UILabel *label = (UILabel *)[cell.contentView viewWithTag:100];
    [label setText:[items objectAtIndex:indexPath.row]];
    
    CustomAddButton *addButton = (CustomAddButton *)[cell.contentView viewWithTag:101];
    [addButton addTarget:self action:@selector(addButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    
    
    if ([tagsArray containsObject:[NSNumber numberWithInt:(int)(101+indexPath.row)]]) {
        NSLog(@"===%@",[NSNumber numberWithInt:(int)(101+indexPath.row)]);
        [addButton setHidden:YES];
        
    }else{
        NSLog(@"---%@",[NSNumber numberWithInt:(int)(101+indexPath.row)]);

        [addButton setHidden:NO];
        
    }
    
    
    return cell;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
}
-(void)addButtonPressed:(UIButton *)btn{
    
   
    CustomAddButton *customAddButton = (CustomAddButton *)btn;
    
    NSLog(@"添加的viewtag ＝%d",customAddButton.viewId);
    
    [delegate didSeletedAddViewIndex:customAddButton.viewId];
    
    [self remove];
}
- (void)remove{
    [self removeFromSuperview];
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
